<?php

class PrisesController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $prises = new Application_Model_DbTable_Prises();
		$this->view->prises = $prises->fetchAll();
    }

    public function ajouterAction()
    {
        $form = new Application_Form_Prise();
		$form->envoyer->setLabel('Ajouter');
		$this->view->form = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$date = $form->getValue('date');
				$distributeur = $form->getValue('distributeur');
				$journale = $form->getValue('journale');
				$prises = new Application_Model_DbTable_Prises();
				$prises->ajouterPrise($date, $distributeur, $journale);

				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		}
    }

    public function modifierAction()
    {
        $form = new Application_Form_Prise();
		$form->envoyer->setLabel('Sauvegarder');
		$this->view->form = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$id = $form->getValue('id');
				$date = $form->getValue('date');
				$distributeur = $form->getValue('distributeur');
				$journale = $form->getValue('journale');
				$prises = new Application_Model_DbTable_Prises();
				$prises->modifierPrise($id, $date, $distributeur, $journale);

				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		} else {
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$prises = new Application_Model_DbTable_Prises();
				$form->populate($prises->obtenirPrise($id));
			}
		}
    }

    public function supprimerAction()
    {
        if ($this->getRequest()->isPost()) {
			$supprimer = $this->getRequest()->getPost('supprimer');
			if ($supprimer == 'Oui') {
				$id = $this->getRequest()->getPost('id');
				$prises = new Application_Model_DbTable_Prises();
				$prises->supprimerPrise($id);
			}

			$this->_helper->redirector('index');
		}
		else {
			$id = $this->_getParam('id', 0);
			$prises = new Application_Model_DbTable_Prises();
			$this->view->prise = $prises->obtenirPrise($id);
		}
    }


}







